package com.gzhryc.common.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.gzhryc.common.config.models.XmlConfigEntity;
import com.gzhryc.common.event.QuartzServer;
import com.gzhryc.common.xml.XmlTools;

public class XmlConfigTools {
	
	static XmlConfigEntity entity;
	static String configFilePath;	//配置文件
	
	/**
	 * 定时扫描文件
	 * @param path
	 * @param seconds
	 */
	public static void load(String path,Long seconds){
		load(path);
		QuartzServer.start();
		QuartzServer.delayedRunJob(QuartzServer.buildCronExpression(seconds), ConfigReader.class);
	}
	
	/**
	 * 读配置文件
	 * @param path
	 */
	public static void load(String path){
		FileReader reader = null;
		StringBuffer sb = new StringBuffer();
		try {
			File file = new File(path);
			if(!file.exists()){file.createNewFile();}
			reader = new FileReader(file);
			char[] cbuf = new char[1024];
			int i = 0;
			while(reader.ready()){
				i = reader.read(cbuf, 0, 1024);
				sb.append(cbuf, 0, i);
			}
			
			configFilePath = path;
			if(sb.length() > 0){
				entity = XmlTools.changToObject(sb.toString(), XmlConfigEntity.class, false);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 写配置文件
	 */
	public static void write(){
		if(entity != null){
			FileWriter writer = null;
			try {
				String xml = XmlTools.changToXmlStr(entity, false);
				writer = new FileWriter(configFilePath);
				writer.write(xml);
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(writer != null){
					try {
						writer.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static XmlConfigEntity getEntity() {
		return entity;
	}

	/**
	 * 添加一个字符串配置
	 * @param id
	 * @param value
	 */
	public static void setString(String id,String value){
		if(entity == null){
			entity = new XmlConfigEntity();
		}
		XmlConfigEntity.Parameter param = null;
		if(entity.getParameters().size() == 0){
			param = new XmlConfigEntity.Parameter();
			param.setName("default");
			entity.getParameters().add(param);
		}else{
			param = entity.getParameters().get(0);
		}
		
		XmlConfigEntity.Item item = getItem(param,id);
		if(item == null){
			item =new XmlConfigEntity.Item();
			item.setId(id);
			item.setInput("text");
			item.setType("String");
			item.setValue(value);
			param.getItem().add(item);
		}else{
			item.setValue(value);
		}
	}
	
	public static XmlConfigEntity.Item getItem(XmlConfigEntity.Parameter param,String id){
		for(XmlConfigEntity.Item item : param.getItem()){
			if(item.getId().equals(id)){
				return item;
			}
		}
		return null;
	}
	
	public static String getString(String id){
		if(entity != null && entity.getParameters() != null){
			for(XmlConfigEntity.Parameter param : entity.getParameters()){
				for(XmlConfigEntity.Item item : param.getItem()){
					if(item.getId().equals(id)){
						return item.getValue();
					}
				}
			}
		}
		return null;
	}
	
	public static Boolean getBoolean(String id){
		if(entity != null && entity.getParameters() != null){
			for(XmlConfigEntity.Parameter param : entity.getParameters()){
				for(XmlConfigEntity.Item item : param.getItem()){
					if(item.getId().equals(id)){
						String value = item.getValue();
						if(StringUtils.isNotBlank(value)){
							return Boolean.valueOf(value);
						}
					}
				}
			}
		}
		return null;
	}

	public static class ConfigReader implements Job{

		@Override
		public void execute(JobExecutionContext arg0) throws JobExecutionException {
			load(configFilePath);
		}
		
	}
}
